﻿using System;
using System.Collections.Generic;
using ExFlash.Actions;
using ExFlash.Records;

namespace ExFlash.Tags
{
    public class DefineButtonTag : Tag, IHasCharacterId
    {
        private readonly int _swfVersion;

        protected int SwfVersion
        {
            get { return _swfVersion; }
        }

        public ushort ButtonId
        {
            get;
            protected set;
        }

        public List<ButtonRecord> Characters
        {
            get;
            protected set;
        }

        public List<ActionRecord> Actions
        {
            get;
            protected set;
        }

        public DefineButtonTag(int len,int version, int offset)
            : this(TagCodes.DefineButton, len, version, offset)
        {
            
        }

        protected DefineButtonTag(TagCodes code, int len, int version, int offset)
            : base(code, len, offset)
        {
            _swfVersion = version;
        }

        public override void SwfDeserialize(BinaryBitReader reader)
        {
            ButtonId = reader.ReadUInt16();

            Characters = GetButtonRecordList(reader);

            Actions = ActionAssistant.GetActions(reader, _swfVersion);
        }

        public override void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        protected List<ButtonRecord> GetButtonRecordList(BinaryBitReader reader)
        {
            List<ButtonRecord>  temp = new List<ButtonRecord>();

            while (reader.PeekByte() != 0)
            {
                ButtonRecord rec = new ButtonRecord(TagCode);
                rec.SwfDeserialize(reader);
                temp.Add(rec);
            }

            reader.Position++;

            return temp;
        }

        #region IHasCharacterID Members

        public ushort Id
        {
            get { return ButtonId; }
        }

        #endregion
    }
}
